<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10">
        <div class="mainor_form">
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="90px"
            id="form"
          >
            <el-form-item label="出库状态：" prop="status">
              <el-select
                v-model.trim="form.status"
                placeholder="出库状态"
                class="wid220"
                @change="getArticleList"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(statusList, index) in list.statusList"
                  :key="index"
                  :label="statusList"
                  :value="parseInt(index + 1)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="出库类型：" prop="type">
              <el-select
                v-model.trim="form.type"
                placeholder="出库类型"
                class="wid220"
                @change="getArticleList"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(typeList, index) in list.typeList"
                  :key="index"
                  :label="typeList"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="出库时间：" prop="createTime">
              <el-date-picker
                v-model="createTime"
                type="daterange"
                class="time220"
                :editable="false"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                :clearable="false"
                :picker-options="pickerOptions"
                @change="getArticleList"
              />
            </el-form-item>
            <MainFormBtn @reset="reset" @getSearchFor="getSearchFor" :loading="listLoading" :showBtn="moredata">
              <el-button @click="exportOutBound">导出</el-button>
            </MainFormBtn>
            <div class="more el-form-item" @click="more">
              <span>更多</span>
              <i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
            </div>
            <div class="clearfix"></div>
            <el-collapse-transition>
              <div v-show="moredata">
                <el-form-item label="出库单号：" prop="order_sn">
                  <el-input
                    v-model.trim="form.order_sn"
                    placeholder="出库单号"
                    clearable
                    class="wid220"
                  ></el-input>
                </el-form-item>
                <el-form-item label="关联单号：" prop="related_order_sn">
                  <el-input
                    v-model.trim="form.related_order_sn"
                    placeholder="关联单号"
                    clearable
                    class="wid220"
                  ></el-input>
                </el-form-item>
                <el-form-item label="创建日期：" prop="establishTime">
                  <el-date-picker
                    v-model="establishTime"
                    type="daterange"
                    class="time220"
                    :editable="false"
                    unlink-panels
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd"
                    :clearable="false"
                    :picker-options="pickerOptions"
                    @change="getArticleList"
                  />
                </el-form-item>
                <div class="clearfix"></div>
                <el-form-item label=" ">
                  <MainFormBtn class="btn-bottom" @reset="reset" @getSearchFor="getSearchFor" :loading="listLoading" :showBtn="!moredata">
                    <el-button @click="exportOutBound">导出</el-button>
                  </MainFormBtn>
                </el-form-item>
              </div>
            </el-collapse-transition>
          </el-form>
        </div>
        <div class="main-controls-wrap">
          <div>
            <router-link to="/warehouse/withdrawal/add" class="mainor_tea">创建出库单</router-link>
          </div>
        </div>
        <div class="main_table">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list.data"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            :header-cell-class-name="this.$verticaline"
            :height="borderHeight"
            :header-cell-style="tableHeightStyle"
            @header-dragend="changeTableWidth"
            @row-dblclick="rowDblclick"
          >
            <el-table-column
              label="出库单号"
              prop="order_sn"
              :width="tableWidth[hash + 'order_sn']"
            />
            <el-table-column
              label="出库类型"
              prop="type"
              :width="tableWidth[hash + 'type']"
            >
              <template slot-scope="scope">{{
                list.typeList[scope.row.type]
              }}</template>
            </el-table-column>

            <el-table-column
              label="关联单号"
              prop="related_order_sn"
              :width="tableWidth[hash + 'related_order_sn']"
            />

            <el-table-column
              label="出库金额"
              prop="money"
              :width="tableWidth[hash + 'money'] || 150"
            />
            <el-table-column
              label="出库状态"
              prop="status"
              :width="tableWidth[hash + 'status'] || 150"
            >
              <template slot-scope="scope"
                ><span :class="'order' + scope.row.status">
                  {{ list.statusList[scope.row.status] }}</span
                ></template
              >
            </el-table-column>
            <el-table-column
              label="创建时间"
              prop="created_at"
              :width="tableWidth[hash + 'created_at']"
            >
              <template slot-scope="scope">{{
                parseTime(scope.row.created_at, "{y}-{m}-{d}")
              }}</template>
            </el-table-column>
            <el-table-column
              label="出库时间"
              prop="updated_at"
              :width="tableWidth[hash + 'updated_at']"
            >
              <template slot-scope="scope">{{
                parseTime(scope.row.stock_out_time, "{y}-{m}-{d}")
              }}</template>
            </el-table-column>

            <el-table-column
              label="管理操作"
              align="center"
              fixed="right"
              prop="operate"
              :width="tableWidth[hash + 'operate'] || 200"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <router-link :to="'/warehouse/withdrawal/see/' + scope.row.id">
                  <el-link type="success">查看</el-link>
                </router-link>
                <el-link
                  style="color: rgb(17, 138, 178)"
                  @click="exportOrderiInfo(scope.row.id)"
                  >导出</el-link
                >
                <el-link @click="print(scope.row.id)">打印</el-link>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <MainPagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
    <!-- 出库单导出 -->
    <ExportDialog :visible.sync="exportVisible" :statusList="list.statusList" :typeList="list.typeList"></ExportDialog>
    <!-- 打印子组件 -->
    <order-print
      ref="printTemplate"
      v-show="false"
      id="printname"
    ></order-print>
  </div>
</template>

<script>
import {
  stockOut_index,
  export_stockout_info,
} from "@/api/warehouse/warehouseWithdrawal";
import { mixins } from "@/mixins/index";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import dateOption from "@/options/dateOption";
import OrderPrint from "@/views/order/orderPrint.vue";
import { parseTime, exportMethod } from "@/utils/index";
import ExportDialog from "@/views/warehouse/components/withdrawal/exportDialog.vue"
export default {
  mixins: [mixins],
  components: { 
    OrderPrint,
    ExportDialog,
  },
  data() {
    return {
      // 时间
      parseTime,
      exportMethod,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: {
        list: [],
        statusList: [],
        typeList: {},
      },
      listLoading: false,
      // 搜索
      form: {
        status: null,
        type: null,
        order_sn: "",
        related_order_sn: "",
        stockOut_start_time: "",
        stockOut_end_time: "",
        start_time: "",
        end_time: "",
      },
      createTime: [],
      establishTime: [],
      pickerOptions: dateOption,
      // 更多
      moredata: false,
      exportVisible: false, // 出库单导出弹窗状态
    };
  },
  activated() {
    // 列表
    this.getNumerical(210);
    this.stockOut_index();
  },
  methods: {
    //  表格双击跳转详情页面
    rowDblclick(row, column, event) {
      this.$router.push('/warehouse/withdrawal/see/' + row.id)
    },
    startGetting() {
      this.getArticleList();
    },
    // 重置
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.establishTime = [];
      this.createTime = [];
      this.getArticleList();
    },
    // 点击搜索时将多余的搜索条件隐藏
    getSearchFor() {
      this.getArticleList();
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.stockOut_index();
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.stockOut_index();
    },
    changePage(page) {
      this.current.page = page;
      this.stockOut_index();
    },
    // 更多
    more() {
      this.moredata = !this.moredata;
    },
    // 列表
    stockOut_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current)
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      stockOut_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.start_time = this.createTime[0] ? this.createTime[0] : "";
      form.end_time = this.createTime[1] ? this.createTime[1] : "";
      form.stockOut_start_time = this.establishTime[0]
        ? this.establishTime[0]
        : "";
      form.stockOut_end_time = this.establishTime[1]
        ? this.establishTime[1]
        : "";

      return form;
    },
    // 导出出库单
    exportOutBound() {
      this.exportVisible = true;
    },
    // 导出
    exportOrderiInfo(id) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: id }
      );
      const notify = this.$notify.info({
        title: "开始导出...",
        type: "success",
        duration: 0,
      });
      export_stockout_info(data)
        .then((res) => {
          this.exportMethod(res, "出库单", notify);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
    },
    // 打印
    print(id) {
      this.$refs.printTemplate.$emit("demo", {
        id: id,
        type: "stockout",
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: 100%;
  }
}

// 支付状态
.success {
  color: $green;
}
.unpaid {
  color: $colour_red;
}
// 订单状态
.order0 {
  color: $order_color1;
}
.order1 {
  color: $order_color2;
}
.order2 {
  color: $order_color3;
}
.order4 {
  color: $order_color4;
}
/deep/.el-form-item--small.el-form-item {
  margin-bottom: 10px;
}
</style>
